package com.platform.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.annotation.RequestBean;
import com.platform.base.PageUtil;
import com.platform.base.R;
import com.platform.base.mvc.BaseController;
import com.platform.dto.SysAreaDTO;
import com.platform.entity.ExcelColumns;
import com.platform.ext.offices.poi.excel.ExcelUtil;
import com.platform.ext.offices.poi.utils.DownLoadUtil;
import com.platform.logger.annotation.ApiLog;
import com.platform.service.SysAreaService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 地区表
 * @author platform
 * @email 15909910367@163.com
 * @date 2022-11-09 20:51:06
 */
@Controller
@RequestMapping("area")
@Api(tags="OMS-行政区划管理")
public class SysAreaController extends BaseController{

    @Autowired
    private SysAreaService sysAreaService;

    @RequestMapping(value="data",method=RequestMethod.GET)
    @ApiOperation(value="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "bean", value = "数据对象", required = true, dataType = "SysAreaDTO"),
        @ApiImplicitParam(name = "start", value = "页码", required = true, dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "每页显示条数", required = true, dataType = "int")
	})
	@ResponseBody
    public R<?> list(@RequestBean SysAreaDTO bean,Integer start,Integer limit){
    	PageUtil pageUtil = new PageUtil();
		pageUtil.setPageIndex(start);
		pageUtil.setPageSize(limit);
		pageUtil.setParams(BeanUtil.beanToMap(bean));
		List<SysAreaDTO> array = sysAreaService.selectByMapPaging(pageUtil);
        return R.success(array,pageUtil.getTotal());
    }
    
    @ApiOperation(value="查询树形结构") 
    @RequestMapping(value="findTreeData",method=RequestMethod.GET)
    @ResponseBody
    public R<?> findTreeDate(String node){
    	List<SysAreaDTO> array = sysAreaService.queryAreaTreeByLazy(node);
    	return R.success(array);
    }

    @ApiOperation(value="获取单条信息")
    @RequestMapping(value="/get/{id}",method=RequestMethod.GET)
    @ResponseBody
    public R<?> info(@PathVariable("id") Long id){
		SysAreaDTO sysArea = sysAreaService.selectByPrimaryKey(id);
        return R.success(sysArea);
    }

    @ApiOperation(value="新增数据")
    @ApiLog(value = "新增行政区划数据",desc = "")
    @RequestMapping(value="save",method=RequestMethod.POST)
    @ResponseBody
    public R<?> save(SysAreaDTO sysArea){
    	sysArea.setCreater(getName());
    	sysArea.setCreateDate(new Date(System.currentTimeMillis()));
        return sysAreaService.insert(sysArea)> 0?R.success():R.fail();
    }

    @ApiOperation(value="修改数据")
    @ApiLog(value = "修改行政区划数据",desc = "")
    @RequestMapping(value="update",method=RequestMethod.POST)
    @ResponseBody
    public R<?> update(SysAreaDTO sysArea){
    	sysArea.setLastModifier(getName());
    	sysArea.setLastModifyDate(new Date(System.currentTimeMillis()));
		return sysAreaService.updateBySelective(sysArea) > 0?R.success():R.fail();
    }

    @ApiOperation(value="删除数据")
    @ApiLog(value = "删除行政区划数据",desc = "")
    @RequestMapping(value="delete",method=RequestMethod.POST)
    @ResponseBody
    public R<?> delete(String ids){
        return sysAreaService.deleteByBatch(ids.split(",")) > 0?R.success():R.fail();
    }
    
    @ApiOperation(value="导出查询结果到Excel表格")
    @ApiLog(desc="",value="导出查询结果到Excel表格")
	@RequestMapping(value = "/exportToExcel",method = {RequestMethod.GET,RequestMethod.POST})
	public void exportToExcel(@RequestParam Map<String,Object> params)  throws IOException{
		//参数解析
    	List<ExcelColumns> columns = JSONUtil.parseArray(params.get("columns").toString()).toList(ExcelColumns.class);
		boolean isPDF = params.get("toPdf") != null?Boolean.parseBoolean(params.get("toPdf").toString()):false;	//是否导出当前页
		String protalParamsStr = params.get("protalParams").toString();
		@SuppressWarnings("unchecked")
		Map<String,Object> protalParams = JSONUtil.toBean(protalParamsStr, Map.class);
	    boolean colorless 	= Boolean.parseBoolean(params.get("colorless").toString());
	    boolean usemonetary = Boolean.parseBoolean(params.get("usemonetary").toString());
	    boolean sumless 	= Boolean.parseBoolean(params.get("sumless").toString());
	    boolean splitCell 	= Boolean.parseBoolean(params.get("splitCell").toString());
	    boolean watermark 	= Boolean.parseBoolean(params.get("watermark").toString());
	    String openPassword = params.get("openPassword").toString();
	    String userName		= params.get("userName").toString();
	    String deptname		= params.get("deptname").toString();
	    String systemName	= params.get("systemName").toString();
	    String modularName = params.get("modularText").toString();
		//生成
		PageUtil pg = new PageUtil();
		pg.setPageIndex(Integer.valueOf(params.get("currentPage").toString()));
		pg.setPageSize(Integer.valueOf(params.get("pageSize").toString()));
		pg.setParams(protalParams);
	    List<SysAreaDTO> exportData = sysAreaService.selectByMapPaging(pg);
		OutputStream osExcel = ExcelUtil.generateExcelOrPDF(modularName,exportData,protalParams,columns,colorless,usemonetary,sumless,splitCell,watermark,openPassword,userName,deptname,systemName);
		if(isPDF) {
			OutputStream osPDF = DownLoadUtil.excelStreamToPDFStream(new ByteArrayInputStream(((ByteArrayOutputStream) osExcel).toByteArray()));
			String fn = modularName + "列表.pdf";
			download(osPDF, fn, response);
		}else {
			String fn = modularName + "列表.xls";
			download(osExcel, fn, response);
		}
	}
}
